package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ResPath;
	import engine.logic.tips.TipsManage;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.MainUIManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;

	/**
	 * 作画功能
	 * @author 
	 * 
	 */
	public class DrawPictureLogic
	{
		private var _backMc		:MovieClip
		private var _myDis			:DrawPictureUI;
		private var _model			:ModelLocator;
		private var _oldPicture	:MovieClip;
		private var _penMc			:MovieClip;
		private var _clickType		:int;
		private var _vipLevel		:int
		private var _picIndex		:int;
		
		private static var _self:DrawPictureLogic
		
		public function DrawPictureLogic()
		{
			init();
		}
		public static function getInstance():DrawPictureLogic
		{
			if(_self == null)
			{
				_self	=	new DrawPictureLogic();
			}
			return _self;
		}
		public function BeforeShow():void
		{
			
			if(_backMc == null)
			{
				completeHandle();
//				MultLoader.getInstance().load([ResPath.SWF + "DrawPic.swf"],true,completeHandle);
//				SwfLoader.StartLoad(ResPath.SWF + "DrawPic.swf",null,completeHandle)
			}else
			{
				addListent();
			}
//			if(DrawInfoUI.getInstance().isOk)
//			{
//				DrawInfoUI.getInstance().hide();
//				setTimeout(function():void{showInfoUI()},500);
//			}
				
		}
		/**
		 * 当前玩家的VIP等级 
		 * @param val  8级以下不显示最高级获取按钮
		 * 
		 */		
		public function set vipLevel(val:int):void
		{
			_vipLevel	=	val;
			if(_vipLevel < 5)
			{
				_myDis.MaxBtn.visible	=	false;
				_myDis.smallBtn.x		=	247;
				_myDis.middBtn.x		=	388;
			}
			else
			{
				_myDis.MaxBtn.visible	=	true;
				_myDis.smallBtn.x		=	177;
				_myDis.middBtn.x		=	318;
			}
		}
		/**
		 * 关闭侦听事件
		 */		
		public function removeListent():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CIMILIA_REPUTE);
			_myDis.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,windowCloseHandle);
			removeBtnEvent(_myDis.smallBtn);
			removeBtnEvent(_myDis.middBtn);
			removeBtnEvent(_myDis.MaxBtn);
			_oldPicture	=	null;
			if(_backMc != null)
			{
				removeBtnEvent(_backMc.getChildByName("pic1") as MovieClip);
				removeBtnEvent(_backMc.getChildByName("pic2") as MovieClip);
				removeBtnEvent(_backMc.getChildByName("pic3") as MovieClip);
				removeBtnEvent(_backMc.getChildByName("pic4") as MovieClip);
				removeBtnEvent(_backMc.getChildByName("pic5") as MovieClip);
//				if(!DrawInfoUI.getInstance().isOk)
//				{
					for(var i:int = 1; i <= 5; i++)
					{
						pictureMove(i , 0);
					}
					
//				}
				
				_penMc.gotoAndStop(1);
			}
		}
		/**
		 * 开始侦听事件
		 */		
		private function addListent():void
		{
			_myDis.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,windowCloseHandle)
			addBtnEvent(_myDis.smallBtn);
			addBtnEvent(_myDis.middBtn);
			addBtnEvent(_myDis.MaxBtn);
			addBtnEvent(_backMc.getChildByName("pic1") as MovieClip);
			addBtnEvent(_backMc.getChildByName("pic2") as MovieClip);
			addBtnEvent(_backMc.getChildByName("pic3") as MovieClip);
			addBtnEvent(_backMc.getChildByName("pic4") as MovieClip);
			addBtnEvent(_backMc.getChildByName("pic5") as MovieClip);
//			if(!DrawInfoUI.getInstance().isOk)
//			{
				for(var i:int = 1; i <= 5; i++)
				{
					pictureMove(i , 1);
				}
				_penMc.gotoAndStop(1);
//			}
			

		}
		/**
		 * 初始化
		 */		
		private function init():void
		{
			
			
			_model	=	ModelLocator.getInstance();
			_myDis	=	DrawPictureUI.getInstance();
			//获取的作品号
			_model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEARCH_CIMILIA ),drawPicID);
			//换一副的信息
			_model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHANGE_LUCK_CIMILIA ),changePic);
			//换一副没有成功的消息
			_model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_CIMILIA_CHANGE_LUCK_FAILED ),changCanNotPic);

			_myDis.frame.mouseChildren	=	_myDis.frame.mouseEnabled	=	false;
			BindManage.getInstance().BindProp(_model.scene.Role,"VIPLevel",this,"vipLevel");
			_myDis.middBtn.enabled =true ; 
			_myDis.MaxBtn.enabled =true ; 
			_myDis.smallBtn .enabled =true ; 
		}
		/**
		 * 临摹图的信息
		 * @param e
		 * 
		 */		
		private function drawPicID(e:CustomMsgEvent):void
		{
			
			var picIndexStr:String	=	String(e.pList[0]);
			_picIndex	=	int(picIndexStr.substr(picIndexStr.length - 1,1));
//			DrawInfoUI.getInstance().isOk = true ; 
			drawMove(_picIndex);
			_myDis.middBtn.enabled =false ; 
			_myDis.MaxBtn.enabled =false ; 
			_myDis.smallBtn .enabled =false ; 
		}
		/**
		 * 显示提示信息 
		 */		
		private function showInfoUI():void
		{
			DrawInfoUI.getInstance().showInfo(_picIndex,_clickType,showInfoFunction);
		}
		/**
		 * 回调函数 
		 * @param val
		 * @return 
		 * 
		 */		
		private function showInfoFunction(val:int):void
		{
			if(val  == 0)
			{
				//确定收下此图片
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CIMILIA_REPUTE);
//				DrawPictureUI.getInstance().showOrHide();
				DrawPictureUI.getInstance().hide();
			}
			else
			{
				//换一副图片
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_CHANGE_CIMILIA);
			}
			
		}
		/**
		 * 换一副的信息 
		 * @param e
		 * 
		 */		
		private function changePic(e:CustomMsgEvent):void
		{
			DrawInfoUI.getInstance().hide();
			var picIndexStr:String	=	String(e.pList[1])
			DrawInfoUI.getInstance().hide();
			_picIndex	=	int(picIndexStr.substr(picIndexStr.length - 1,1));
			drawMove(_picIndex);
		}
		/**
		 * 改运不成功的 
		 * @param e
		 * 
		 */
		private function changCanNotPic(e:CustomMsgEvent):void
		{
			DrawInfoUI.getInstance().hide();
			drawMove(_picIndex);
//			_penMc.gotoAndStop(1);
//			
//			(_penMc.getChildByName("pic") as MovieClip).gotoAndStop(_picIndex);
//			
//			pictureMove(_picIndex,0,
//			function ():void
//			{
//				nextFrame(_penMc,
//					function ():void
//					{
//						pictureMove(_picIndex,1)
//					});
//			});
		}
		/**
		 * 加载完背景内容后 
		 * @param dis
		 * @param data
		 * 
		 */		
		private function completeHandle():void
		{
			var dis:MovieClip=MultLoader.getInstance().getSourceByUrl(ResPath.SWF + "DrawPic.swf");
			_backMc	=	dis;
			
			while(_myDis.BackMc.numChildren > 0)
			{
				_myDis.BackMc.removeChildAt(0);
			}
			_myDis.BackMc.addChild(_backMc);
			_penMc	=	dis.getChildByName("drawPicMc") as MovieClip;
			addListent();
		}
		/**
		 * 添加按钮的侦听事件 
		 * @param target
		 * 
		 */		
		private function addBtnEvent(target:DisplayObject):void
		{
			
			target.addEventListener(MouseEvent.CLICK,		btnClickHandle);
			target.addEventListener(MouseEvent.MOUSE_OVER,	btnOverHandle);
			target.addEventListener(MouseEvent.MOUSE_OUT,	btnOutHandle);
		}
		/**
		 * 移除按钮事件
		 * @param target
		 * 
		 */		
		private function removeBtnEvent(target:DisplayObject):void
		{
			target.removeEventListener(MouseEvent.CLICK,		btnClickHandle);
			target.removeEventListener(MouseEvent.MOUSE_OVER,	btnOverHandle);
			target.removeEventListener(MouseEvent.MOUSE_OUT,	btnOutHandle);
		}
		/**
		 * 按钮点击
		 */		
		private function btnClickHandle(e:MouseEvent):void
		{
			var target:DisplayObject	=	e.currentTarget as DisplayObject
			if(target == _myDis.smallBtn || target == _myDis.middBtn || target == _myDis.MaxBtn)
			{
				if(_model.scene.Role.GetPropertiesEx("SearchCimiliaNums") > 0)
				{
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("99168"));//"今日次数已用完！"
					return;
				}
			}
			if(DrawInfoUI.getInstance().created==true)
			{
				return  ; 
			}
			switch(target)
			{
				case _myDis.smallBtn:
					//初级
					_clickType	=	0;
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_GET_CIMILIA,0);
					break;
				case _myDis.middBtn:
					//中级
					if(SitAlerUI.getInstance().created == false)
					{ 	
						SitAlerLogic.getIntatic().setUIinfo("UIString_1000505",[],middBtnHandler,_myDis.middBtn)
					}
					
					break;
				case _myDis.MaxBtn:
					//最高级
					if(SitAlerUI.getInstance().created == false)
					{ 	
						SitAlerLogic.getIntatic().setUIinfo("UIString_1000506",[],MaxBtnHandler,_myDis.MaxBtn)
					}
					break;
			}
		}
		private function middBtnHandler():void
		{
			_clickType	=	1;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_GET_CIMILIA,1);
		}
		private function MaxBtnHandler():void
		{
			_clickType	=	2;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLEINT_CUSTOMMSG_GET_CIMILIA,2);
		}
		/**
		 * 鼠标移入
		 */		
		private function btnOverHandle(e:MouseEvent):void
		{
			var target:DisplayObject	=	e.currentTarget as DisplayObject
			switch(target)
			{
				case _myDis.smallBtn:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000403"));
					break;
				case _myDis.middBtn:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000404"));
					break;
				case _myDis.MaxBtn:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStr("UIString_1000405"));
					break;
				case _backMc["pic1"]:
				case _backMc["pic2"]:
				case _backMc["pic3"]:
				case _backMc["pic4"]:
				case _backMc["pic5"]:
					TipsMainLogic.instance.showString(StaticIni.getSwfLangStrVar("UIString_1000406",
						[DrawInfoUI.getInstance().getPicName(int(target.name.substr(target.name.length - 1,1))),DrawInfoUI.getInstance().getReput(int(target.name.substr(target.name.length - 1,1)))]))
					break;
			}
		}
		/**
		 * 鼠标移出
		 */		
		private function btnOutHandle(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		/**
		 * 关闭窗口 
		 * @param e
		 * 
		 */		
		private function windowCloseHandle(e:NxWindowEvent):void
		{
			
			_myDis.hide();
		}
		/**
		 * 播放绘图的全过程动画 
		 * @param val
		 * 
		 */		
		private function drawMove(val:int):void
		{
			for(var i:int = 1; i <= 5; i++)
			{
				pictureMove(i , 0);
			}
			_penMc.gotoAndStop(1);
			
			(_penMc.getChildByName("pic") as MovieClip).gotoAndStop(val);
			nextFrame(_penMc,
				function ():void
				{
					pictureMove(val,1,showInfoUI)
				});
		}
		/**
		 * 播放图片动画  
		 * @param val  对象
		 * @param type 播放的方法
		 * 
		 */		
		private function pictureMove(val:int,type:int,backFun:Function = null):void
		{
			if(_backMc != null)
			{
				var moveMc:MovieClip	=	_backMc.getChildByName("pic" + val) as MovieClip;
				if(type	==	0)
				{
					preFrame(moveMc,backFun);
				}
				else if(type == 1)
				{
					nextFrame(moveMc,backFun);
				}
			}
		}
		/**
		 * 回播
		 * @param target
		 * 
		 */		
		private function preFrame(target:MovieClip,backFunc:Function = null):void
		{
			
			target.addEventListener(Event.ENTER_FRAME,move);
				
			function move(e:Event):void
			{
				if(target.currentFrame == 1)
				{
					target.removeEventListener(Event.ENTER_FRAME,move);
					if(backFunc != null)
					{
						backFunc.call();
					}
				}
				else
				{
					target.gotoAndStop(target.currentFrame -1);
				}
			}
		}
		/**
		 * 正播 
		 * @param e
		 * 
		 */		
		private function nextFrame(target:MovieClip,backFunc:Function = null):void
		{
			target.addEventListener(Event.ENTER_FRAME,move);
			
			function move(e:Event):void
			{
				if(target.currentFrame == target.totalFrames -1)
				{
					target.removeEventListener(Event.ENTER_FRAME,move);
					if(backFunc != null)
					{
						backFunc.call();
					}
				}
				else
				{
					target.gotoAndStop(target.currentFrame +1);
				}
			}
		}
	}
}